﻿using System;
using MailClient;
using MailClient.Data;
using MailClient.Data.IndexTables;
using nBayes;

namespace MailClient.Commands
{
    public enum DeliverableTypes { None, Merch, Shell, Water_Report, Fuel_Study, Geotech, Brand_Review, City_Approval, Design_Civil };

    /// <summary>
    /// Class for no command
    /// </summary>
    class none : command, ICommand, IDisposable
    {
        public none(string[] query) { }

        private MailMessage _msg;

        public MailMessage MsgReply
        {
            get { return _msg; }
        }

        public void Dispose() { }

        public bool CommitData() { return false; }

        public bool Command(Job job)
        {
            /*
             * Use Nbayes filtering to try to filter 
             * and guess delieverables here's how:
             * There are <#> deliverables
             * Merch, Shell, water report, fuel study, geotech, brand review, city approval, deisgn civil drawings
             * Since Nbayes uses 2 indecies at a time a return 1 of three results, 
             * we can group our deliverables and try guessing.
             * each iteration should refine the guess until we get it right ( or the email isn't a delieverable)
             * iteration 1:
             * group 1: Merch
             * group 2: shell
             * iteration 2: depends on if undecided is returned
             * group 1: water report
             * group 2: fuel study
             * iteration 3: depends on undecided:
             * group 1: brand review
             * group 2: city approval
             * iteration 4: depends on undecided
             * group 1: geotech
             * group 2: design civil
             */


            Entry document = Entry.FromString(job.TestString);

            // Training tr = new Training(new string[] { "merch" });
            //tr.Command(job);

            DeliverableTypes type = DetermineType(document, DeliverableTypes.Merch, DeliverableTypes.Shell);
            // we should now know what kind of email this is. now we need to find out what store is belongs to then
            // act accordingly. the job for this should have the store on it.
            if (type != DeliverableTypes.None)
            {
                ICommand c = base.Parse(type);
                if (c.Command(job))
                    c.CommitData();
            }

            // for now lets just make it reply with an email noting what we think that email was
            _msg = new MailMessage();
            _msg.Subject = string.Format("Re: {0}", job.TestString);
            //_msg.Subject = "merch";
            _msg.Body = "That email was a " + type.ToString();

            // replies to Doug or myself.  later we can implement a way to reply 
            // to the sender a recieved confirmation
            var to = new System.Net.Mail.MailAddress("bcrawford@rhaaia.com");
            var from = new System.Net.Mail.MailAddress("rha@rhaaia.com", "RHA Email");
            _msg.Sender = from;
            return true;

        }

        private DeliverableTypes DetermineType(Entry document, DeliverableTypes type1, DeliverableTypes type2)
        {
            FileIndex idx1 = ResolveIndex(type1);
            FileIndex idx2 = ResolveIndex(type2);
            DeliverableTypes deliverableresult = DeliverableTypes.None;

            Analyzer analyzer = new Analyzer();
            CategorizationResult result = analyzer.Categorize(
    document, idx1, idx2);
            if (result == CategorizationResult.Undetermined)
            {
                if (type1 == DeliverableTypes.Merch)
                { deliverableresult = DetermineType(document, DeliverableTypes.Water_Report, DeliverableTypes.Fuel_Study); }
                else if (type1 == DeliverableTypes.Water_Report)
                { deliverableresult = DetermineType(document, DeliverableTypes.Brand_Review, DeliverableTypes.City_Approval); }
                else if (type1 == DeliverableTypes.Brand_Review)
                {
                    deliverableresult = DetermineType(document, DeliverableTypes.Geotech, DeliverableTypes.Design_Civil);
                    if (result == CategorizationResult.Undetermined) return DeliverableTypes.None;
                }
            }

            switch (result)
            {
                case CategorizationResult.First:
                    // is idx1
                    idx1.Add(document);
                    return type1;
                case CategorizationResult.Second:
                    //is idx2
                    idx2.Add(document);

                    return type2;
                default:
                    // don't know
                    return deliverableresult;
            }
        }

        private FileIndex ResolveIndex(DeliverableTypes type)
        {

            switch (type)
            {
                case DeliverableTypes.Brand_Review:
                    return BrandIndex.Index;
                case DeliverableTypes.City_Approval:

                    return CityIndex.Index;
                case DeliverableTypes.Design_Civil:

                    return CivilIndex.Index;
                case DeliverableTypes.Fuel_Study:

                    return FuelIndex.Index;
                case DeliverableTypes.Geotech:

                    return GeoIndex.Index;
                case DeliverableTypes.Merch:
                    return MerchIndex.Index;
                case DeliverableTypes.Shell:

                    return ShellIndex.Index;
                case DeliverableTypes.Water_Report:

                    return WaterIndex.Index;
                default:
                    return null;
            }
        }

        private Handlers.designator _designator;
        public Handlers.designator Designator
        {
            get { return _designator; }
            set { _designator = Designator; }
        }
    }
}
